<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Class HsCode
 * @package App\Models
 * @property Unit $un1
 * @property Unit $un2
 */
class HsCode extends Model
{
    protected $primaryKey = 'code';
    protected $keyType = 'string';
    public $timestamps = false;
    protected $fillable = ['code', 'name', 'unit1', 'unit2', 'element'];

    protected function setElementAttribute($value)
    {
        $this->attributes['element'] = implode('|', $value);
    }

    protected function getElementAttribute($value)
    {
        return explode('|', $value);
    }

    /**
     * @return BelongsTo
     */
    public function un1(){
        return $this->belongsTo(Unit::class, 'unit1', 'code');
    }

    /**
     * @return BelongsTo
     */
    public function un2(){
        return $this->belongsTo(Unit::class, 'unit2', 'code');
    }
}
